package com.jy.api;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by lihu on 2017/12/29.
 */
@Configuration
@EnableAsync
public class MyThread {


    @Value("${executor.core.pool-size}")
    private int corePoolSize;//线程池维护线程的最少数量

    @Value("${executor.max.pool-size}")
    private int maxPoolSize;//线程池维护线程的最大数量

    @Value("${executor.queue.capacity}")
    private int queueCapacity; //缓存队列

    @Value("${executor.keep.alive}")
    private int keepAlive;//允许的空闲时间

    @Bean
    public Executor myExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("mqExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
        executor.setKeepAliveSeconds(keepAlive);
        executor.initialize();
        return executor;
    }

}
